﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ADO.NET_WinFrom
{   
    public partial class index : Form
    {
        string constr = ConfigurationManager.ConnectionStrings["Usersconn"].ConnectionString;

        public index()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 进入首页加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {

            //using (SqlConnection conn = new SqlConnection(constr))
            //{
            //    string sql = "SELECT uid, upwd, uname, usex, uaddress, utel, uage FROM userInfo";

            //    using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
            //    {
            //        DataTable dt = new DataTable();

            //        sda.Fill(dt);

            //        this.dataGridView1.DataSource = dt;
            //    }
            //}
            indexLoad();
        }
        /// <summary>
        /// 首页表格数据加载
        /// </summary>
        private void indexLoad()
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = "SELECT uid, upwd, uname, usex, uaddress, utel, uage FROM userInfo";

                using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
                {
                    DataTable dt = new DataTable();

                    sda.Fill(dt);

                    this.dataGridView1.DataSource = dt;
                }
            }
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_add_Click(object sender, EventArgs e)
        {
            string uname = this.uname.Text;
            string upwd  = this.upwd.Text;
            string usex  = this.usex.Text;            
            string uage = this.uage.Text;
            string utel = this.utel.Text;
            string uaddress = this.uaddress.Text;

            using (SqlConnection conn =new SqlConnection(constr))
            {
                string sql = "INSERT INTO userInfo (uname,upwd,usex,utel,uage,uaddress)VALUES(@uname,@upwd,@usex,@utel,@uage,@uaddress)";
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.Parameters.AddWithValue("@uname", uname);
                cmd.Parameters.AddWithValue("@upwd", upwd);
                cmd.Parameters.AddWithValue("@usex", usex);
                cmd.Parameters.AddWithValue("@uage", uage);
                cmd.Parameters.AddWithValue("@utel", utel);
                cmd.Parameters.AddWithValue("@uaddress", uaddress);
                conn.Open();
                if (cmd.ExecuteNonQuery()<=0)
                {
                    MessageBox.Show("添加失败");
                }
                else
                {
                    MessageBox.Show("添加成功");
                    indexLoad();
                }
                conn.Close();
            }

        }
        /// <summary>
        /// 删除选中行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_delete_Click(object sender, EventArgs e)
        {
            //获取当前选中行的id
            string selectID = this.dataGridView1.SelectedRows[0].Cells["uid"].Value.ToString();
            if (selectID==null)
            {
                MessageBox.Show("请先选中要删除的数据行");
                return;
            }
            using (SqlConnection conn=new SqlConnection(constr))
            {
                conn.Open();
                string sql = "delete from userinfo where uid=@uid";
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.Parameters.AddWithValue("@uid",selectID);
                if (cmd.ExecuteNonQuery()>0)
                {
                    MessageBox.Show("删除成功");
                    indexLoad();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                conn.Close();
            }
        }
    }
}
